<template>
  <div class="sidebar-container">
    <!-- 顶部Logo -->
    <div class="logo-container">
      <router-link to="/">
        <img
          src="@/assets/logo.png"
          class="sidebar-logo"
        >
        <h1 class="system-title">
          林风论坛后台
        </h1>
      </router-link>
    </div>

    <!-- 可折叠侧边栏 -->
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :active-text-color="variables.menuActiveText"
        :unique-opened="false"
        :collapse-transition="false"
        mode="vertical"
      >
        <sidebar-item
          v-for="route in permission_routes"
          :key="route.path"
          :item="route"
          :base-path="route.path"
          :is-collapse="isCollapse"
        />
      </el-menu>
    </el-scrollbar>

    <!-- 折叠按钮 -->
    <div
      class="collapse-btn"
      @click="toggleCollapse"
    >
      <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" />
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SidebarItem from './SidebarItem'
import variables from '@/styles/variables.scss'

export default {
  components: { SidebarItem },
  computed: {
    ...mapGetters([
      'permission_routes',
      'sidebar'
    ]),
    activeMenu () {
      const route = this.$route
      const { meta, path } = route
      return meta.activeMenu || path
    },
    isCollapse () {
      return !this.sidebar.opened
    },
    variables () {
      return variables
    }
  },
  methods: {
    toggleCollapse () {
      this.$store.dispatch('app/toggleSideBar')
    }
  }
}
</script>

<style lang="scss" scoped>
.sidebar-container {
  position: relative;
  height: 100%;
  background: $menuBg;
  transition: width 0.28s;

  .logo-container {
    height: 50px;
    padding: 10px 15px;
    overflow: hidden;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);

    .sidebar-logo {
      width: 32px;
      height: 32px;
      vertical-align: middle;
      display: inline-block;
    }

    .system-title {
      display: inline-block;
      margin: 0 0 0 10px;
      color: #fff;
      font-size: 16px;
      font-weight: 600;
      vertical-align: middle;
      white-space: nowrap;
    }
  }

  .scrollbar-wrapper {
    overflow-x: hidden !important;
    height: calc(100% - 50px);

    ::v-deep .el-menu {
      border-right: none;
    }
  }

  .collapse-btn {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #fff;
    background: rgba(0, 0, 0, 0.1);
    cursor: pointer;
    transition: background 0.3s;

    &:hover {
      background: rgba(0, 0, 0, 0.2);
    }

    i {
      font-size: 18px;
    }
  }
}
</style>
